package com.palmwindActivity.textmodule;

import java.util.Vector;
import java.util.concurrent.ThreadPoolExecutor;

import com.palmwindActivity.app.AbstractServiceProxy;
import com.palmwindActivity.app.ICommConstants;
import com.palmwindActivity.app.IServiceProxyConstants;
import com.palmwindActivity.app.IServiceProxyListener;
import com.palmwindActivity.app.RequestParam;
import com.palmwindActivity.proto.ProtoBuf;
import com.palmwindActivity.proto.TextUpload;

public class TextUploadProxy extends AbstractServiceProxy {

	public TextUploadProxy(ThreadPoolExecutor threadpool,
			IServiceProxyListener serverListener) {
		super(threadpool, serverListener);
		// TODO Auto-generated constructor stub
	}

	public void uploadUserComments(String userName, long timeStamp, String comments)
	{
		RequestParam param = new RequestParam(IServiceProxyConstants.ACTION_UPLOAD_TEXT, this);
		param.params = new Vector();
		param.params.add(userName);
		param.params.add(Long.valueOf(timeStamp));
		param.params.add(comments);
		Vector requestVector= this.createProtoBufReq(param);
		if(requestVector != null && requestVector.size() > 0)
		{
			sendData(requestVector, IServiceProxyConstants.ACTION_UPLOAD_TEXT, 15000, 0);
		}
	}
	
	protected void addRequestArgs(Vector<ProtoBuf> request,
			RequestParam requestItem) {
		if(requestItem.params.size() == 3)
		{
			String userName = (String)requestItem.params.get(0);
			Long timeStamp = (Long)requestItem.params.get(1);
			String comment = (String)requestItem.params.get(2);
			TextUpload.TextUploadReq.Builder builder = TextUpload.TextUploadReq.newBuilder();
			builder.setUploadtime(timeStamp.longValue());
			builder.setAction(IServiceProxyConstants.ACTION_UPLOAD_TEXT);
			builder.setUserName(userName);
			builder.setText(comment);
			TextUpload.TextUploadReq req = builder.build();
			
			ProtoBuf pb = new ProtoBuf();
            pb.setBufferData(req.toByteArray());
            pb.setObjType(IServiceProxyConstants.ACTION_UPLOAD_TEXT);
			request.add(pb);
		}

	}

	protected void handleResponse(ProtoBuf[] response) {
		try {
			if (response != null) {
				int size = response.length;
				for (int i = 0; i < size; i++) {
					TextUpload.TextUploadResp textUploadResp = TextUpload.TextUploadResp
							.parseFrom(response[i].getBufferData());
					int respValue = textUploadResp.getResponse();
					if(respValue != 0)
					{
						respValue = ICommConstants.NETWORK_FAIL;
					}
				}

			}
		} catch (Throwable t) {
			this.status = ICommConstants.NETWORK_PARSER_ERROR;
			t.printStackTrace();
		}

	}

}
